package _interview150;

import _object.TreeNode;

/**
 * 108. 将有序数组转换为二叉搜索树
 */
public class No108 {
    private int[] nums;

    public TreeNode sortedArrayToBST(int[] nums) {
        this.nums = nums;
        return dfs(0, nums.length - 1);
    }

    private TreeNode dfs(int left, int right) {
        if (left > right) return null;
        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = dfs(left, mid - 1);
        root.right = dfs(mid + 1, right);
        return root;
    }
}
